<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Tag_Models_Mysql_Tag extends Modules_Tag_Models_Tag
{
	protected $_table = 'tag';
	
	public function getById($id)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE tag_id = ' . $this->_conn->escape($id);
		$sql .= ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function add($tag)
	{
		$tagId = $this->_conn->insert($tag, $this->_conn->_tablePrefix . 'tag');
		return $tagId;	
	}
	
	public function checkExist($text, $tagId = null)
	{
		$sql = 'SELECT COUNT(*) AS num_tags FROM ' . $this->_conn->_tablePrefix . 'tag AS t
					WHERE tag_text = "' . $this->_conn->escape($text) . '"';
		if ($tagId) {
			$sql .= ' AND tag_id != "' . $this->_conn->escape($tagId) . '"';
		}
		$sql .= ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return ($row['num_tags'] > 0) ? true : false;
	}
	
	public function update($article)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . '  
					SET tag_text = "%s", slug = "%s"
					WHERE tag_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($article['tag_text']),
							$this->_conn->escape($article['slug']),
							$this->_conn->escape($article['tag_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function find($offset, $limit, $condition = array())
	{
		$sql = 'SELECT t.* FROM ' . $this->_conn->_tablePrefix . 'tag AS t';
		
		if (!empty($condition)) {
			$sql .= ' WHERE 1';
		}
		
		if (isset($condition['tag_text']) && $condition['tag_text']) {
			$sql .= ' AND t.tag_text LIKE "%' . $this->_conn->escape($condition['tag_text']) . '%"';
		}
		
		$sql .= ' ORDER BY t.created_date DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$tags = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$tags[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $tags;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) AS num_tags FROM ' . $this->_conn->_tablePrefix . 'tag AS t';
		
		if (!empty($condition)) {
			$sql .= ' WHERE 1';
		}
		
		if (isset($condition['tag_text']) && $condition['tag_text']) {
			$sql .= ' AND t.tag_text LIKE "%' . $this->_conn->escape($condition['tag_text']) . '%"';
		}
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_tags'];
	}
	
	public function delete($id)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE tag_id = ' . $this->_conn->escape($id);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function getByItem($item)
	{
		$sql  = sprintf("SELECT t.tag_id, t.tag_text, ti.details_route_name
						FROM " . $this->_conn->_tablePrefix . $this->_table . " AS t
						INNER JOIN " . $this->_conn->_tablePrefix . "tag_item_assoc AS ti
							ON t.tag_id = ti.tag_id
						WHERE ti.item_id = '%s'
							AND ti.item_name = '%s'
							AND ti.route_name = '%s'
							AND ti.details_route_name = '%s'
						GROUP BY t.tag_id",
						$this->_conn->escape($item['item_id']),
						$this->_conn->escape($item['item_name']),
						$this->_conn->escape($item['route_name']),
						$this->_conn->escape($item['details_route_name']));
						
		$rs   = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		$this->_conn->freeResult($rs);
		return $rows;		
	}
}